<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

require APPPATH . '/libraries/BaseController.php';
/**
 * Class : User (UserController)
 * User Class to control all user related operations.
 * @author : Kishor Mali
 * @version : 1.1
 * @since : 15 November 2016
 */
class Orders extends BaseController
{
	/**
     * This is default constructor of the class
     */
    public function __construct()
    {
        parent::__construct();
        $this->isLoggedIn();   
        $this->load->helper(array('form', 'url'));
    }

    public function index(){
        $this->global['pageTitle'] = '订单管理';
        $this->load->model('order_model');
        $this->load->library('pagination');
        $count = $this->order_model->orderListingCount();
        $returns = $this->paginationCompress ( "orders/", $count, 10);
        $data['orders'] = $this->order_model->orderListing($returns["page"], $returns["segment"]);
        $this->loadViews("orders", $this->global, $data, NULL);

    }

    public function delete(){
    	$this->load->model('order_model');
    	$orderId = $this->input->post('id');
    	$this->order_model->deleteOrder($orderId);
    	$this->success('OK');
    }


    public function createProduct($id = 0){

    	$this->global['pageTitle'] = '订单管理';
        // $orders = $this->goods_model->getAll($number, ($page - 1) * $number);
       

        $this->loadViews("ordersForm/productForm", $this->global, array('order_id' => $id));

    }

    public function createAmazon($id = 0){

    	$this->global['pageTitle'] = '订单管理';
        // $orders = $this->goods_model->getAll($number, ($page - 1) * $number);
       

        $this->loadViews("ordersForm/amazonForm", $this->global, array('order_id' => $id));
    	
    }

    public function codes($id = 0) {
    	$this->global['pageTitle'] = '订单管理';
    	$this->loadViews("ordersForm/amazonForm", $this->global, array('order_id' => $id, 'codes' => true));
    }

    public function getProducts(){
    	$this->load->model('order_model');
    	$this->response($this->order_model->getProducts());
    }



    public function saveProductOrder(){
    	$this->load->model('order_model');
    	$order = $this->input->post('order');

    	if(!isset($order['created'])) {
    		$order['created'] = $this->vendorId;
    	}
    	if(!isset($order['order_type'])) {
    		$order['order_type'] = 1;
    	}
    	if(!isset($order['order_state'])) {
    		$order['order_state'] = 1;
    	}
    	if(!isset($order['id'])) {
    		$order['id'] = null;
    	}
    	if(!isset($order['remark'])) {
    		$order['remark'] = '';
    	}
    	$orderId = $this->order_model->saveProductOrder($order);
    	$this->response(array(
    		'order_id' => $orderId
    	));
    }

    public function getProductOrder() {
    	$order_id = $this->input->get('id');
    	$this->load->model('order_model');
    	$order = $this->order_model->getProductOrder($order_id);
    	$this->response($order);
    	
    }

    public function exportProducts($id) {
    	$this->load->library("hei_excel");
    	$order_id = $this->input->post('id');
    	$columns = $this->input->post('columns');
    	$source = $this->input->post('source');

    	$this->load->model('order_model');
    	$orders = $this->order_model->$source($order_id);

        $data = [];
        $row = 1;
        $col_config = [];
        $col_width = [];
        $col_bold = [];
        foreach ($columns as $col) {
        	$col_name = chr($col['col'] + 64);
        	$data[$col_name.$row] = $col['label'];
        	$col_config[$col['prop']] = $col;
        	$col_width[$col_name]  = isset($col['width']) ? $col['width'] : 10;
        	array_push($col_bold, $col_name);
        }
        
        foreach ($orders['products'] as $key => $order) {
        	$row++;
        	foreach ($order as $prop => $value) {
        		if(isset($col_config[$prop])) {
        			$col_name = chr($col_config[$prop]['col'] + 64);
        			$data[$col_name.$row] = $value;
        		}
        	}
        }
        $options = [
            'print' => true,//打印格式
            'alignCenter' => array_keys($data),//居中
            'setWidth' => $col_width,
            'bold' => $col_bold,
        ];
        $this->hei_excel->exportExcel($data, '黑爷爷.xlsx', $options);
    }

    public function saveAmazonOrder(){
    	$this->load->model('order_model');
    	$order = $this->input->post('order');

    	if(!isset($order['created'])) {
    		$order['created'] = $this->vendorId;
    	}
    	if(!isset($order['order_type'])) {
    		$order['order_type'] = 2;
    	}
    	if(!isset($order['order_state'])) {
    		$order['order_state'] = 10;
    	}
    	if(!isset($order['id'])) {
    		$order['id'] = null;
    	}
    	if(!isset($order['remark'])) {
    		$order['remark'] = '';
    	}
    	$orderId = $this->order_model->saveAmazonOrder($order);
    	$this->response(array(
    		'order_id' => $orderId
    	));
    }

    public function getAmazonOrder() {
    	$order_id = $this->input->get('id');
    	$this->load->model('order_model');
    	$order = $this->order_model->getAmazonOrder($order_id);
    	$this->response($order);
    	
    }

}


?>